import axios from "./request";
import { IErrorData, ISuccessData, ISuccessPageData } from "./returnData";

export interface ILogin {
    loginId: string,
    loginPwd: string,
    captcha?: string,
    isOldPassword?: boolean
}

export interface IUsers {
    loginId: string,
    loginPwd: string,
    name: string,
    oldLoginPwd: string
}

export interface IReturnUser {
    loginId: string,
    name: string,
    id: string
}


/**
 * 获取验证码
 */
export function getCapatach(): Promise<any> {
    return axios.get("/res/captcha");
}

// 用户登录
export function userLogin(login: ILogin): Promise<ISuccessData<any> | IErrorData> {
    return axios.post("/api/admin/login", login);
}

/**
 * 我是谁
 * @returns 
 */
export function fetchWhoAmi(): Promise<ISuccessData<any> | IErrorData> {
    return axios.get("/api/admin/whoami");
}

// 文件上传
export function upload(file: any) {
    return axios.post("/api/upload", file);
}

/**
 * 修改管理员内容
 * @param user 
 * @returns 
 */
export function updateUser(user: IUsers): Promise<ISuccessPageData<IReturnUser> | IErrorData> {
    return axios.put('/api/admin', user);
}